﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication.BaseClass;

namespace WebApplication.MessageContent
{
    public class MessageContext:IMessageContext
    {
        private int _maxRecordCount;

        public string UserName { get; set; }

        public DateTime LastActiveTime { get; set; }

        public MessageContainer<IRequestMessageBase> RequestMessage { get; set; }


        public MessageContainer<IResponseMessageBase> ResponseMessage { get; set; }

        public int MaxRecordCount
        {
            get
            {
                return _maxRecordCount;
            }
            set
            {
                RequestMessage.MaxReocrdCount = _maxRecordCount;
                ResponseMessage.MaxReocrdCount = _maxRecordCount;
                MaxRecordCount = _maxRecordCount;
            }
        }

        public object StorageData { get; set; }

        public event EventHandler<WeixinContextRemovedEventArgs> MessageContextRemoved = null;


        public MessageContext()
        {
            RequestMessage = new MessageContainer<IRequestMessageBase>(MaxRecordCount);
            ResponseMessage = new MessageContainer<IResponseMessageBase>(MaxRecordCount);
            LastActiveTime = DateTime.Now;
        }

        public virtual void OnRemoved()
        {
            var onRemoveArg = new WeixinContextRemovedEventArgs(this);
            OnMessageContextRemoved(onRemoveArg);
        }


        private void OnMessageContextRemoved(WeixinContextRemovedEventArgs e)
        {
            EventHandler<WeixinContextRemovedEventArgs> temp = MessageContextRemoved;

            if (temp != null)
            {
                temp(this, e);
            }
        }


    }
}